/*
 * ConnectDlg.java
 *
 */
package app;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;

public class ChartDlg extends JDialog {

    public ChartDlg( Frame owner, Map<EarningDate, List<SingleIVPoint>> ivData) {
        super( owner, true);
		JPanel containerPanel = new JPanel();
		
        for (Entry<EarningDate, List<SingleIVPoint>> entry : ivData.entrySet()) {
        	XYDataset data = getDataSet(entry.getValue());
	        JFreeChart chart = ChartFactory.createTimeSeriesChart(
	        		entry.getKey().date.toString(),
	                "Date", 
	                "Value",
	                data, 
	                true, 
	                true, 
	                false
	            );
			ChartPanel panel = new ChartPanel(chart);
			
			containerPanel.add(panel);
        }
		
        add(containerPanel);
        pack();
		setVisible(true);
    }
    
    private XYDataset getDataSet(List<SingleIVPoint> ivPoints) {
    	final TimeSeriesCollection dataset = new TimeSeriesCollection();
    	
    	final TimeSeries timeSeries = new TimeSeries("series");
    	
    	for (SingleIVPoint ivPoint : ivPoints) {
    		if (ivPoint.date != null) {
    			timeSeries.add(new Day(ivPoint.date), ivPoint.IV);
    		}
    	}
    	
    	dataset.addSeries(timeSeries);
		return dataset;
	}
}